<?php

/**
 * Helper for creating URLs for redirects and other tasks
 *
 * @uses       Zend_Controller_Action_Helper_Abstract
 * @category   Zend
 * @package    Zend_Controller
 * @subpackage Zend_Controller_Action
 * @copyright  Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class WSB_Controller_Helper_Navigation extends Zend_Controller_Action_Helper_Abstract
{

	/**
	 * @var array
	 */
	private $_links;

	/**
	 * Bei Forwards wuerde die Navigation mehrmals ausgegeben. Diese statische
	 * Variable soll dies verhindern.
	 *
	 * @var booleand
	 */
	static $_rendered = false;

	public function __construct()
	{
		$this->_links = array();
	}

	/**
	 * @param string $href
	 * @param string $label
	 */
	public function add($href, $label)
	{
        array_push($this->_links, array('href' => $href, 'label' => $label));
	}

	/**
	 * @return void
	 */
    public function preDispatch()
	{
		$this->render();
    }

	/**
	 * @return void
	 */
    public function clearAll()
	{
		$this->_links = array();
	}

	/**
	 * @return void
	 */
    public function render()
	{
    	if(!self::$_rendered) {
			// Listlinks
			if(count($this->_links) > 0) {
			    foreach($this->_links AS $index=>$link) {
			        $str = stristr($_SERVER['REQUEST_URI'], $link['href']);
			        if(!empty($str)) {
			            $this->_session->subnavActiveIndex = $index;
			        }
			    }
			    $html = '';
			    foreach($this->_links AS $index=>$link) {
			        if(isset($this->_session->subnavActiveIndex) && ($this->_session->subnavActiveIndex == $index)) {
			            // Aktiv
	                    $html .= sprintf('<li><a class="active" href="%s">%s</a></li>', $link['href'], $link['label']);
			        } else {
			            // Inaktiv
	                    $html .= sprintf('<li><a class="inactive" href="%s">%s</a></li>', $link['href'], $link['label']);
			        }
			    }
	            $this->getResponse()->appendBody(sprintf('<div class="subnavigation"><ul>%s</ul><div style="clear:both;"></div></div>', $html));
			}
			self::$_rendered = true;
    	}
    }

}